package com.hlingsoft.kkh.action.admin;

import com.hlingsoft.kkh.dao.GuessDao;
import com.hlingsoft.kkh.entity.Guess;
import com.hlingsoft.kkh.entity.vo.JsonVo;
import com.hlingsoft.kkh.entity.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/guess")
public class AdminGuessAction extends AdminBaseAction {

	@Autowired
	private GuessDao guessDao;

	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) {

		PageVo<Guess> pageVo = new PageVo<Guess>(pageNum);
		pageVo.setRows(20);
		List<Guess> list = guessDao.getGuessList(pageVo.getOffset(),
				pageVo.getRows());
		pageVo.setList(list);
		pageVo.setCount(guessDao.getGuessListCount());
		modelMap.put("pageVo", pageVo);
		return "system/guess/all";
	}

	
	@RequestMapping(value = "/toAdd.htm", method = RequestMethod.GET)
	public String toAdd(HttpServletRequest request,
			ModelMap modelMap) {
		modelMap.put("kindId",System.currentTimeMillis());
		modelMap.put("JSESSIONID", request.getSession().getId());
		return "system/guess/add";
	}

	
	
	@RequestMapping(value = "/delete.json" )
	@ResponseBody
	public JsonVo<String> delete(@RequestParam(value = "guessId") long guessId,
			ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		guessDao.deleteGuess(guessId);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/add.json")
	@ResponseBody
	public JsonVo<String> add(@RequestParam(value = "name") String name,
			@RequestParam(value = "url") String url,
			@RequestParam(value = "code") String code,
			@RequestParam(value = "logo") String logo,
			@RequestParam(value = "description") String description,
			ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		json.setResult(true);
		Guess guess = new Guess();
		guess.setDescription(description);
		guess.setName(name);
		guess.setUrl(url);
		guess.setCode(code);
		guess.setLogo(logo);
		guessDao.addGuess(guess);
		return json;
	}

}
